﻿using Microsoft.ManagementConsole.SnapIns.Controls;
using Microsoft.ManagementConsole.SnapIns.Integration;
using PlanckTime.Entity;
using System;
using Veripark.Data;

namespace Microsoft.ManagementConsole.Controls
{
    public class PlanckTimeNode : ScopeNode
    {
        #region Fields&Properties

        public const string Description = "PlanckTime Node";

        public Task[] Tasks { get; set; }

        #endregion

        #region Methods

        public PlanckTimeNode()
        {
        }

        public PlanckTimeNode(Guid guid)
            : base(guid)
        {
        }

        protected override void OnAddPropertyPages(PropertyPageCollection propertyPageCollection)
        {
            propertyPageCollection.Add(new PlanckTimePropertyPage(this));
        }

        private void InitializeNodeData()
        {
            if (this.Tasks == null && this.Tag != null)
            {
                var taskColleciton = PlanckTimeServices.Instance.GetTasks(this.Tag as Task);

                if (taskColleciton != null)
                {
                    this.Tasks = taskColleciton.ConvertTo<Task>();

                    foreach (var item in this.Tasks)
                    {
                        var node = new PlanckTimeNode(item.Id);
                        node.EnabledStandardVerbs = StandardVerbs.Properties;
                        FormViewDescription frm = new FormViewDescription();
                        
                        frm.ViewType = typeof(PlanckTimeFormView);
                        frm.ControlType = typeof(PlanckTimeTaskDetailControl);
                        frm.DisplayName = item.DateCreated.ToShortDateString();
                        frm.Tag = item;

                        node.ViewDescriptions.Add(frm);


                        node.DisplayName = item.Name;
                        node.Tag = item;

                        this.Children.Add(node);
                    }
                }
            }
        }


        protected override void OnExpand(AsyncStatus status)
        {
            InitializeNodeData();
        }

        public void Refresh()
        {
            this.Tasks = null;
            InitializeNodeData();
        }

        #endregion
    }
}
